using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using OgilvyOne.Model;
using OgilvyOne.BLL;
using OgilvyOne.IDAL;

namespace OgilvyOne.ProcessFlow
{
    public class SysMenuController
    {
        // Fields
        private string bgColor = "#EEEEEE";
        private string sLeafImageUrl = "";
        private string sNodeImageUrl = "";
        private const string URL_DEFAULT = "/systemmanage/sysmenu/sysmenu_list.aspx?refresh=1";
        private const string URL_ITEMCREATE = "/systemmanage/sysmenu/sysmenu_add.aspx";
        private const string URL_ITEMUPDATE = "/systemmanage/sysmenu/sysmenu_modify.aspx";

        // Methods
        public bool CreateMenu(SysMenuInfo newMenuInfo)
        {
            try
            {
                new SysMenu().Insert(newMenuInfo);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public bool CreateRoleTreeView(TreeView tv, string sName, string sPwd, bool isLink)
        {
            try
            {
                Tree tree = new Tree();
                ITree tree2 = OgilvyOne.DALFactory.SysMenu.Create();
                if (!this.NodeImageUrl.Equals(""))
                {
                    tree.NodeImageUrl = this.NodeImageUrl;
                }
                if (!this.LeafImageUrl.Equals(""))
                {
                    tree.LeafImageUrl = this.LeafImageUrl;
                }
                TreeNode tNode = tree.CreateRoot(tv, tree2, isLink);
                tree.RoleTreeViewBind(tNode, int.Parse(tNode.Value), tree2, sName, sPwd, isLink);
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.ToString());
                return false;
            }
            return true;
        }

        public bool CreateTreeView(TreeView tv)
        {
            try
            {
                Tree tree = new Tree();
                ITree tree2 = OgilvyOne.DALFactory.Tree.MenuCreate();
                if (!this.NodeImageUrl.Equals(""))
                {
                    tree.NodeImageUrl = this.NodeImageUrl;
                }
                if (!this.LeafImageUrl.Equals(""))
                {
                    tree.LeafImageUrl = this.LeafImageUrl;
                }
                TreeNode tNode = tree.CreateRoot(tv, tree2);
                tree.TreeViewBind(tNode, int.Parse(tNode.Value), tree2);
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.ToString());
                return false;
            }
            return true;
        }

        public bool CreateTreeView(TreeView tv, int root_id, int con_id)
        {
            try
            {
                Tree tree = new Tree();
                ITree tree2 = OgilvyOne.DALFactory.SysMenu.Create();
                if (!this.NodeImageUrl.Equals(""))
                {
                    tree.NodeImageUrl = this.NodeImageUrl;
                }
                if (!this.LeafImageUrl.Equals(""))
                {
                    tree.LeafImageUrl = this.LeafImageUrl;
                }
                TreeNode tNode = tree.CreateRoot(tv, root_id, tree2);
                tree.DownloadTreeViewBind(tNode, root_id, tree2, con_id);
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.ToString());
                return false;
            }
            return true;
        }

        public bool CreateTreeView(TreeView tv, string sName, string sPwd, bool isLink)
        {
            try
            {
                Tree tree = new Tree();
                ITree tree2 = OgilvyOne.DALFactory.SysMenu.Create();
                if (!this.NodeImageUrl.Equals(""))
                {
                    tree.NodeImageUrl = this.NodeImageUrl;
                }
                if (!this.LeafImageUrl.Equals(""))
                {
                    tree.LeafImageUrl = this.LeafImageUrl;
                }
                TreeNode tNode = tree.CreateRoot(tv, tree2, isLink);
                tree.TreeViewBind(tNode, int.Parse(tNode.Value), tree2, sName, sPwd, isLink);
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.ToString());
                return false;
            }
            return true;
        }

        public bool CreateTreeView(TreeView tv, string addUrl, string id, string IsDel)
        {
            try
            {
                Tree tree = new Tree();
                ISysMenu menu = OgilvyOne.DALFactory.SysMenu.Create();
                if (!this.NodeImageUrl.Equals(""))
                {
                    tree.NodeImageUrl = this.NodeImageUrl;
                }
                if (!this.LeafImageUrl.Equals(""))
                {
                    tree.LeafImageUrl = this.LeafImageUrl;
                }
                TreeNode tNode = tree.CreateRoot(tv, addUrl);
                tree.TreeViewBind(tNode, 0, addUrl, id, menu, IsDel);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public bool CreateTreeView(TreeView tv, string addUrl, string uptUrl, string delUrl, string IsDel)
        {
            try
            {
                Tree tree = new Tree();
                ISysMenu menu = OgilvyOne.DALFactory.SysMenu.Create();
                if (!this.NodeImageUrl.Equals(""))
                {
                    tree.NodeImageUrl = this.NodeImageUrl;
                }
                if (!this.LeafImageUrl.Equals(""))
                {
                    tree.LeafImageUrl = this.LeafImageUrl;
                }
                TreeNode tNode = tree.CreateRoot(tv, addUrl, uptUrl, delUrl);
                tree.TreeViewBind(tNode, 0, addUrl, uptUrl, delUrl, menu, IsDel);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public void DeleteMenu(int iNodeID)
        {
            new SysMenu().Delete(iNodeID);
            HttpContext.Current.Response.Redirect(URL_DEFAULT, true);
        }

        public string getCurrMenuName(int id)
        {
            string str = "";
            try
            {
                SysMenu menu = new SysMenu();
                TreeInfo info = (TreeInfo)menu.getCurrNode("0", id)[0];
                str = info.Node_Desc;
            }
            catch
            {
                return "";
            }
            return str;
        }

        public string getMenuTreeList(int pID, int iLayer, int func_uid)
        {
            string str = "";
            string str2 = "";
            try
            {
                int num;
                Tree tree = new Tree();
                IList list = OgilvyOne.DALFactory.SysMenu.Create().getChildNodeList("0", pID);
                IList list2 = OgilvyOne.DALFactory.MenuFunc.Create().getMenuFuncUIDList(func_uid);
                string str3 = "";
                for (num = 0; num < iLayer; num++)
                {
                    str2 = str2 + "&nbsp;&nbsp;&nbsp;&nbsp;";
                }
                for (num = 0; num < list.Count; num++)
                {
                    TreeInfo info = (TreeInfo)list[num];
                    str = str + "<tr>";
                    str = str + "<td width=\"586\" bgcolor=\"" + this.bgColor + "\" align=\"left\">" + str2 + info.Node_Name + "</td>";
                    str3 = "";
                    for (int i = 0; i < list2.Count; i++)
                    {
                        MenuFuncInfo info2 = (MenuFuncInfo)list2[i];
                        if (info2.Menu_UID == info.iNode_ID)
                        {
                            str3 = " checked";
                        }
                    }
                    str = string.Concat(new object[] { str, "<td width=\"94\" bgcolor=\"", this.bgColor, "\"><input type=checkbox name=chk value=", info.iNode_ID, str3, "></td>" });
                    str = str + "</tr>";
                    str = str + this.getMenuTreeList(info.Node_ID, iLayer + 1, func_uid);
                }
            }
            catch
            {
                return str;
            }
            return str;
        }

        public int SelectMenu(string menu_name)
        {
            SysMenu menu = new SysMenu();
            return menu.Select(menu_name);
        }

        public int GetPID(string menu_name)
        {
            SysMenu menu = new SysMenu();
            return menu.ParentID(menu_name);
        }

        public void UpdateMenu(SysMenuInfo updatedMenuInfo)
        {
            new SysMenu().Update(updatedMenuInfo);
            HttpContext.Current.Response.Redirect(URL_DEFAULT, true);
        }

        public bool ModifyByIdentity(SysMenuInfo data)
        {
            return OgilvyOne.DALFactory.SysMenu.Create().ModifyByIdentity(data);
        }

        // Properties
        public string LeafImageUrl
        {
            get
            {
                return this.sLeafImageUrl;
            }
            set
            {
                this.sLeafImageUrl = value;
            }
        }

        public string NodeImageUrl
        {
            get
            {
                return this.sNodeImageUrl;
            }
            set
            {
                this.sNodeImageUrl = value;
            }
        }

    }
}
